"""
支付渠道基类
定义所有支付渠道必须实现的接口
"""

from abc import ABC, abstractmethod
from typing import Dict, Any, Optional
import frappe


class PaymentChannelBase(ABC):
    """支付渠道基类"""
    
    def __init__(self, config: Dict[str, Any]):
        """
        初始化支付渠道
        
        Args:
            config: 支付渠道配置信息
        """
        self.config = config
        self.channel_name = self.__class__.__name__.replace('Channel', '').lower()
    
    @abstractmethod
    def create_buyer(self, email: str, display_name: str, country: str, **kwargs) -> Dict[str, Any]:
        """
        创建买家
        
        Args:
            email: 买家邮箱
            display_name: 显示名称
            country: 国家代码
            **kwargs: 其他参数
            
        Returns:
            Dict: 买家信息，包含 buyer_id
        """
        pass
    
    @abstractmethod
    def create_checkout(self, buyer_id: str, amount: float, currency: str, **kwargs) -> Dict[str, Any]:
        """
        创建结账表单
        
        Args:
            buyer_id: 买家ID
            amount: 支付金额
            currency: 货币类型
            **kwargs: 其他参数
            
        Returns:
            Dict: 结账信息，包含 checkout_id
        """
        pass
    
    @abstractmethod
    def create_payment_intent(self, checkout_id: str, **kwargs) -> Dict[str, Any]:
        """
        创建支付意图
        
        Args:
            checkout_id: 结账ID
            **kwargs: 其他参数
            
        Returns:
            Dict: 支付意图信息，包含 payment_token 和 payment_url
        """
        pass
    
    @abstractmethod
    def get_payment_status(self, checkout_id: str) -> Dict[str, Any]:
        """
        获取支付状态
        
        Args:
            checkout_id: 结账ID
            
        Returns:
            Dict: 支付状态信息
        """
        pass
    
    @abstractmethod
    def handle_webhook(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        """
        处理 Webhook 通知
        
        Args:
            payload: Webhook 数据
            
        Returns:
            Dict: 处理结果
        """
        pass
    
    def validate_config(self) -> bool:
        """
        验证配置是否完整
        
        Returns:
            bool: 配置是否有效
        """
        required_fields = self.get_required_config_fields()
        for field in required_fields:
            if field not in self.config or not self.config[field]:
                frappe.logger().error(f"[{self.channel_name}] 缺少必要配置: {field}")
                return False
        return True
    
    @abstractmethod
    def get_required_config_fields(self) -> list:
        """
        获取必需的配置字段列表
        
        Returns:
            list: 必需配置字段列表
        """
        pass
    
    def get_channel_info(self) -> Dict[str, Any]:
        """
        获取支付渠道信息
        
        Returns:
            Dict: 渠道信息
        """
        return {
            "channel_name": self.channel_name,
            "display_name": self.get_display_name(),
            "supported_currencies": self.get_supported_currencies(),
            "supported_payment_methods": self.get_supported_payment_methods(),
            "is_active": self.config.get('is_active', True)
        }
    
    @abstractmethod
    def get_display_name(self) -> str:
        """获取显示名称"""
        pass
    
    @abstractmethod
    def get_supported_currencies(self) -> list:
        """获取支持的货币列表"""
        pass
    
    @abstractmethod
    def get_supported_payment_methods(self) -> list:
        """获取支持的支付方式列表"""
        pass
